﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Globalization;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

using System.Web;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.Office.Server.UserProfiles;

namespace JobUsersSync3.Features.JobUsersSync3
{

    [Guid("1ACC99DC-BD4A-4A5D-B230-7687BDF4E2AA")]
    public class JobUsersSync3 : SPJobDefinition
    {

        #region Propertys

        private string _DisplayName =
@"(Ko) Job Users Profiles Sync 3, ver. 3.0.0.1";
        public override string DisplayName { get{ return _DisplayName; }}

        private string _Description =
@"(Ko) Job Users Profiles Sync 3, ver. 3.0.0.1";
        public override string Description { get { return _Description; } }

        #endregion Propertys
        #region Members
        public enum UserLocation { UserOffice = 1, UserNotOffice = 2 }; 
        DataTable adUsersOfficeTbl = null;
        DataTable adUsersNotOfficeTbl = null;
        DataTable BirthdaysUsersOfficeTbl = null;
        DataTable BirthdaysUsersNotOfficeTbl = null;
        Ko2.Sys.AD ad = new Ko2.Sys.AD();
        Ko2.Portal.Env env = new Ko2.Portal.Env();
        Ko2.DB.OneC OneC = new Ko2.DB.OneC();
        string DomainName = new Ko2.Portal.Env().GetAppSetting("DomainName");
        #endregion Members
        #region Common methods
        public string AllTrim(String mStr)
        {
            String vStr = mStr.Replace(" ", string.Empty);
            return vStr;
        }
        public string ConverDate(String mDate)
        {
            String vDate = "";
            //--
            int mCMP = 0;
            if ((mCMP = mDate.IndexOf(".")) != -1)
            {
                vDate = mDate.Substring(6, 4) + "-" + mDate.Substring(3, 2) + "-" + mDate.Substring(0, 2);
            }
            else
            {
                vDate = mDate;
            }
            //--
            return vDate;
        }
        public string InValue(object mValue)
        {
            String mStr;
            if (mValue == null)
                mStr = "";
            else
                mStr = mValue.ToString();
            return mStr;
        }
        #endregion
        #region Constructors
        public JobUsersSync3() : base()
        {
            this.Title = "(Ko) Job Users Profiles Sync 3";
        }
        public JobUsersSync3(string jobName, SPService service, SPServer server, SPJobLockType targetType) : base(jobName, service, server, targetType)
        {
            this.Title = "(Ko) Job Users Profiles Sync 3";
        }
        public JobUsersSync3(string jobName, SPWebApplication webApplication) : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "(Ko) Job Users Profiles Sync 3";
        }
        #endregion Constructors
        #region Job Methods
        public Exception Execute(Guid contentDbId, string siteStr)
        {
            Exception ret = null;
            Dictionary<DateTime, string> InfDict = new Dictionary<DateTime, string>();

            //-------------------------------------------------
            // Get user list : adUsersOfficeTbl : adUsersNotOfficeTbl
            ret = GetUsersLists(ref InfDict);
            if (ret != null)
                return ret;

            //-------------------------------------------------
            // Birthdays list - all items deleting
            ret = SPList_BirthdaysAll_DeleteAll();
            if (ret != null)
                return ret;

            //-------------------------------------------------
            // Update profiles
            InfDict = new Dictionary<DateTime, string>();
            ret = VerifyOfficeUsers(InfDict);
            ret = VerifyNotOfficeUsers(InfDict);

            //-------------------------------------------------
            // Lost SP Accounts
            ret = VerifyLostSPAccounts();

            //-------------------------------------------------
            // New users
            ret = VerifyNewSPAccounts();

            return ret;
        }
        public override void Execute(Guid contentDbId)
        {
            Exception err = Execute(contentDbId, "http://localhost");
        }
        #endregion Job Methods
        #region Users Verify methods
        public Exception VerifyOfficeUsers(Dictionary<DateTime, string> parInf)
        {
            Exception ret = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    using (SPSite site = new SPSite(@"http://localhost/"))
                    {
                        // foreach office users :
                        foreach (DataRow r in adUsersOfficeTbl.Rows)
                        {
                            Exception ret2 = UpdateSPUser(parInf, site, r, UserLocation.UserOffice);
                            if (ret2 != null)
                            {
                                // err save to log
                                parInf.Add(DateTime.Now, ret2.Message);
                                ret = ret2;
                                ret2 = null;
                                continue;
                            }
                        } // foreach
                    } // using site
                } // try
                catch (Exception err)
                {
                    ret = new Exception(err.Message, err);
                }
            });
            return ret;
        }        
        public Exception VerifyNotOfficeUsers(Dictionary<DateTime, string> parInf)
        {
            Exception ret = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    using (SPSite site = new SPSite(@"http://localhost/"))
                    {
                        // foreach office users :
                        foreach (DataRow r in adUsersNotOfficeTbl.Rows)
                        {
                            Exception ret2 = UpdateSPUser(parInf, site, r, UserLocation.UserNotOffice);
                            if (ret2 != null)
                            {
                                // err save to log
                                parInf.Add(DateTime.Now, ret2.Message);
                                ret = ret2;
                                ret2 = null;
                                continue;
                            }
                        } // foreach
                    } // using site
                } // try
                catch (Exception err)
                {
                    ret = new Exception(err.Message, err);
                }
            });
            return ret;
        }
        public Exception UpdateSPUser(Dictionary<DateTime, string> parInf, SPSite site, DataRow r, UserLocation usertype)
        {
            #region Members
            Exception ret = null;
            SPServiceContext context = SPServiceContext.GetContext(site);
            UserProfileManager uprofileManager = new UserProfileManager(context);
            UserProfile up;
            int Change = 0;
            string LoginStr = "";
            string mStrLog = "";
            string LoginFullStr = "";
            string acl = "";
            string ADate = "", BDate = "";
            string Name1 = "", Name2 = "", Name3 = "", DispName = "", OfficeName = "", AccountControl = "";
            string ID1C = "", LoginName = "", Email = "", Phone = "", Otdel = "", JOB = "";
            bool IsOffice = false;
            if(usertype == UserLocation.UserOffice)
                IsOffice = true;
            DateTime dtNow = DateTime.Now;
            DateTime dtBDStart = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day);
            DateTime dtTmp = dtNow.AddDays(5);
            DateTime dtBDEnd = new DateTime(dtTmp.Year, dtTmp.Month, dtTmp.Day);
            #endregion
            try
            {
                #region Init
                LoginStr = "";
                LoginFullStr = "";
                acl = "";
                ADate = ""; BDate = "";
                Name1 = ""; Name2 = ""; Name3 = ""; DispName = ""; OfficeName = ""; AccountControl = "";
                ID1C = ""; LoginName = ""; Email = ""; Phone = ""; Otdel = ""; JOB = "";
                #endregion
                #region Login :
                if (r["Login"] != DBNull.Value)
                    LoginStr = (string)r["Login"];
                if (String.IsNullOrEmpty(LoginStr))
                    return null;
                LoginFullStr = DomainName + "\\" + LoginStr;
                #endregion
                #region acl :
                if (r["USERACCOUNTCONTROL"] != DBNull.Value)
                    acl = (string)r["USERACCOUNTCONTROL"];
                if (!(acl == "512" || acl == "66048"))
                    return null;
                #endregion
                #region Get User Profile or new SPUser
                if (uprofileManager.UserExists(LoginFullStr))
                {
                    up = uprofileManager.GetUserProfile(LoginFullStr);
                    mStrLog += " ; Edit User: " + LoginStr;
                }
                else
                {
                    up = uprofileManager.CreateUserProfile(LoginFullStr);
                    mStrLog += " ; Add User: " + LoginStr;
                }
                #endregion
                #region parse from AD
                if (r["SN"] != DBNull.Value)
                    Name1 = (string)r["SN"];
                if (r["GIVENNAME"] != DBNull.Value)
                    Name2 = (string)r["GIVENNAME"];
                if (r["EXTENSIONATTRIBUTE3"] != DBNull.Value)
                    Name3 = (string)r["EXTENSIONATTRIBUTE3"];
                if (r["DISPLAYNAME"] != DBNull.Value)
                    DispName = (string)r["DISPLAYNAME"];
                if (r["PHYSICALDELIVERYOFFICENAME"] != DBNull.Value)
                    OfficeName = (string)r["PHYSICALDELIVERYOFFICENAME"];
                if (r["USERACCOUNTCONTROL"] != DBNull.Value)
                    AccountControl = (string)r["USERACCOUNTCONTROL"];
                if (r["EXTENSIONATTRIBUTE4"] != DBNull.Value)
                    ID1C = (string)r["EXTENSIONATTRIBUTE4"];
                if (r["Login"] != DBNull.Value) // SAMACCOUNTNAME
                    LoginName = (string)r["Login"];
                if (r["EMailAddress"] != DBNull.Value)
                    Email = (string)r["EMailAddress"];
                if (r["TELEPHONENUMBER"] != DBNull.Value)
                    Phone = (string)r["TELEPHONENUMBER"];
                if (r["Office"] != DBNull.Value)
                    Otdel = (string)r["Department"]; // Department
                if (r["TITLE"] != DBNull.Value)
                    JOB = (string)r["TITLE"];
                #endregion
                #region parse BDate and DateArb from 1C
                ret = GetDateBirth(ref BDate, ID1C);
                if (ret != null)
                    return ret;
                ret = GetDateArb(ref ADate, ID1C);
                if (ret != null)
                    return ret;
                #endregion BDate and DateArb
                //-----------------------------
                Change = 0;
                #region WorkPhone and WorkEmail and ID1C
                if (InValue(up["WorkPhone"].Value).Trim() != Phone.Trim())
                {
                    up["WorkPhone"].Value = Phone;
                    mStrLog += " WorkPhone " + Phone;
                    Change++;
                }
                if (InValue(up["WorkEmail"].Value).Trim() != Email.Trim())
                {
                    up["WorkEmail"].Value = Email;
                    mStrLog += " WorkEmail " + Email;
                    Change++;
                }
                if (InValue(up["ID1C"].Value).Trim() != ID1C.Trim())
                {
                    up["ID1C"].Value = ID1C;
                    mStrLog += " ID1C " + ID1C + " OLD ID1C: " + InValue(up["ID1C"].Value);
                    Change++;
                }
                #endregion WorkPhone and WorkEmail and ID1C
                #region Office, Department, SPS-JobTitle, Title, FirstName, LastName, SPS-PhoneticDisplayName, PreferredName
                if (InValue(up["Office"].Value).Trim() != OfficeName.Trim())
                {
                    up["Office"].Value = OfficeName.Trim();
                    mStrLog += " Office " + OfficeName.Trim();
                    Change++;
                }
                if (InValue(up["Department"].Value).Trim() != Otdel.Trim())
                {
                    up["Department"].Value = Otdel.Trim();
                    mStrLog += " Department " + Otdel.Trim();
                    Change++;
                }
                if (AllTrim(InValue(up["SPS-JobTitle"].Value)) != AllTrim(JOB))
                {
                    String mOld = InValue(up["SPS-JobTitle"].Value);
                    up["SPS-JobTitle"].Value = JOB;
                    mStrLog += " SPS-JobTitle " + JOB + " SPS-JobTitle(Old) " + mOld;
                    Change++;
                }
                if (InValue(up["Title"].Value).Trim() != JOB.Trim())
                {
                    up["Title"].Value = JOB;
                    mStrLog += " Title " + JOB;
                    Change++;
                }
                if (InValue(up["FirstName"].Value).Trim() != Name2.Trim())
                {
                    up["FirstName"].Value = Name2;
                    mStrLog += " FirstName " + Name2;
                    Change++;
                }
                if (InValue(up["LastName"].Value).Trim() != Name1.Trim())
                {
                    up["LastName"].Value = Name1;
                    mStrLog += " LastName " + Name1;
                    Change++;
                }
                if (InValue(up["SPS-PhoneticDisplayName"].Value) != "")
                {
                    up["SPS-PhoneticDisplayName"].Value = "";
                    mStrLog += " SPS-PhoneticDisplayName = '' ";
                    Change++;
                }
                if (InValue(up["PreferredName"].Value).Trim() != (Name1 + " " + Name2 + " " + Name3).Trim())
                {
                    up["PreferredName"].Value = Name1 + " " + Name2 + " " + Name3;
                    mStrLog += " PreferredName " + Name1 + " " + Name2 + " " + Name3;
                    Change++;
                }
                #endregion
                #region ADate, BDate

                DateTime dt_adate;
                if ( !String.IsNullOrEmpty(ADate) )
                    dt_adate = DateTime.Parse(ADate);
                DateTime dt_sp_adate = new DateTime();
                if (up["SPS-HireDate"].Value != null)
                {
                    dt_sp_adate = (DateTime)up["SPS-HireDate"].Value;
                    dt_sp_adate = new DateTime(dtBDStart.Year, dt_sp_adate.Month, dt_sp_adate.Day);
                }
                if ( !String.IsNullOrEmpty(ADate) && ConverDate(InValue(up["SPS-HireDate"].Value)).Trim() != ADate.Trim())
                {
                    up["SPS-HireDate"].Value = ADate;
                    dt_sp_adate = (DateTime)up["SPS-HireDate"].Value;
                    dt_sp_adate = new DateTime(dtBDStart.Year, dt_sp_adate.Month, dt_sp_adate.Day);
                    mStrLog += " SPS-HireDate " + ADate;
                    Change++;
                }

                DateTime dt_bdate;
                if ( !String.IsNullOrEmpty(BDate) )
                    dt_bdate = DateTime.Parse(BDate);
                DateTime dt_sp_bdate = new DateTime();
                if (up["SPS-Birthday"].Value != null)
                {
                    dt_sp_bdate = (DateTime)up["SPS-Birthday"].Value;
                    dt_sp_bdate = new DateTime(dtBDStart.Year, dt_sp_bdate.Month, dt_sp_bdate.Day);
                }
                if ( !String.IsNullOrEmpty(BDate) && ConverDate(InValue(up["SPS-Birthday"].Value)) != BDate.Trim() )
                {
                    up["SPS-Birthday"].Value = BDate;
                    dt_sp_bdate = (DateTime)up["SPS-Birthday"].Value;
                    dt_sp_bdate = new DateTime(dtBDStart.Year, dt_sp_bdate.Month, dt_sp_bdate.Day);
                    mStrLog += " SPS-Birthday " + BDate;
                    Change++;
                }
                if (dt_sp_bdate != null && dt_sp_bdate >= dtBDStart && dt_sp_bdate <= dtBDEnd)
                {
                    Ko2.Portal.SP_List.SPList_BirthdaysAll o = new Ko2.Portal.SP_List.SPList_BirthdaysAll();
                    using (SPWeb web = site.OpenWeb())
                    {
                        string phone = "";
                        if(r["TELEPHONENUMBER"] != DBNull.Value)
                            phone = (string)r["TELEPHONENUMBER"];
                        o.Insert(dt_sp_bdate, up, IsOffice
                            , InValue(up["WorkEmail"].Value).Trim()
                            , InValue(up["PictureURL"].Value).Trim()
                            , OfficeName
                            , Otdel
                            , InValue(up["SPS-JobTitle"].Value).Trim()
                            , phone);
                    }
                }
                #endregion ADate, BDate
                #region ReMake Photo
                //if (File.Exists(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\photo\" + ID1C.Trim() + ".jpg"))
                //{
                //    if (InValue(up["PictureUrl"].Value).Trim() != sUrl + "/_layouts/photo/" + ID1C.Trim() + ".jpg")
                //    {
                //        up["PictureUrl"].Value = sUrl + "/_layouts/photo/" + ID1C.Trim() + ".jpg";
                //        //mStrLog += " Foto " + sUrl + "/_layouts/photo/" + ID1C.Trim() + ".jpg";
                //        Change++;
                //    }
                //}
                /* 
                if (InValue(up["PictureURL"].Value) != ttt)
                {
                    up["PictureURL"].Value = ttt;
                    CreateLineLog(contentDbId, "Change User " + LoginName + " PictureURL " + ttt);
                    Change++;
                }
                */
                {
                    string urltmp = InValue(up["PictureURL"].Value).Trim().ToLower();
                    if( String.IsNullOrEmpty(urltmp) 
                        || urltmp.Trim()==@"http://localhost//_layouts/images/no-photo.png"
                        || urltmp.Trim()==@"http://sp.korablik.local/_layouts/photo/.jpg")
                    {
                        up["PictureURL"].Value = @"http://sp.korablik.local/_layouts/IMAGES/no-photo.png";
                        Change++;
                    }
                }

                #endregion Photo
                #region Save changes
                if (Change > 0)
                    up.Commit();
                //if (up.PersonalSite == null)
                //    up.CreatePersonalSite();
                #endregion Save changes
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }
        public Exception VerifyLostSPAccounts()
        {
            Exception ret = null;
            DateTime DT = DateTime.Now;
            DT = new DateTime(DT.Year, DT.Month, DT.Day);
            List<string> errList = new List<string>();
            Ko2.Web.Utils.WS_Helper hlp = new Ko2.Web.Utils.WS_Helper();
            DataTable tblAD_allUsers = null;
            DataTable tblAD_allSPUsers = null;
            ret = hlp.GetADUsersListALL(ref tblAD_allUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }
            ret = hlp.GetSPUsersList(ref tblAD_allSPUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }

            // проверка 
            // не корректные SP учетки - ID1C == null - HireDate == null
            //
            Ko2.Portal.SP_List.SPList_errSPUsers oListHlp = new Ko2.Portal.SP_List.SPList_errSPUsers();
            oListHlp.DeleteAll();
            foreach (DataRow r in tblAD_allSPUsers.Rows)
            {
                if (r["ID1C"] == DBNull.Value)
                {
                    // save error
                    InsertErrSPUsers(DT, r);
                    continue;
                }
                else
                {
                    string ID1C = (string)r["ID1C"];
                    if (String.IsNullOrEmpty(ID1C))
                    {
                        // save error
                        InsertErrSPUsers(DT, r);
                        continue;
                    }
                }

                if (r["HireDate"] == DBNull.Value)
                {
                    // save error
                    InsertErrSPUsers(DT, r);
                    continue;
                }
                else
                {
                    string HireDate = (string)r["HireDate"].ToString();
                    if (String.IsNullOrEmpty(HireDate))
                    {
                        // save error
                        InsertErrSPUsers(DT, r);
                        continue;
                    }
                }
            }
            return ret;
        }
        public Exception VerifyNewSPAccounts()
        {
            Exception ret = null;
            DateTime DT = DateTime.Now;
            DT = new DateTime(DT.Year, DT.Month, DT.Day);
            DateTime DTStart = DT.AddDays(-14);
            DateTime DTEnd = DT.AddDays(2);
            List<string> errList = new List<string>();
            Ko2.Web.Utils.WS_Helper hlp = new Ko2.Web.Utils.WS_Helper();
            DataTable tbl_allSPUsers = null;
            ret = hlp.GetSPUsersList(ref tbl_allSPUsers, ref errList);
            if (ret != null)
            {
                return new Exception(ret.Message, ret);
            }

            Ko2.Portal.SP_List.SPList_NewUsers oListHlp = new Ko2.Portal.SP_List.SPList_NewUsers();
            oListHlp.DeleteAll();
            foreach (DataRow r in tbl_allSPUsers.Rows)
            {
                if (r["HireDate"] != DBNull.Value)
                {
                    DateTime HireDate = (DateTime)r["HireDate"];
                    if (HireDate >= DTStart && HireDate <= DTEnd)
                    {
                        DateTime HireDate2 = new DateTime(DTStart.Year, HireDate.Month, HireDate.Day);
                        string[] splitChar = { @"\" };
                        string AccountName = "";
                        string Login = "";
                        string UserProfile_GUID = "";
                        string ID1C = "";
                        string ImageUrl = "";
                        string SiteUrl = "";
                        bool IsOffice = false;
                        string OfficeName = "";
                        string DepartmentName = "";
                        string jobName = "";
                        string Phone = "";
                        string Mail = "";
                        if (r["AccountName"] != DBNull.Value)
                            AccountName = (string)r["AccountName"];
                        if (!String.IsNullOrEmpty(AccountName))
                        {
                            string[] s = AccountName.Split(splitChar, StringSplitOptions.None);
                            if (s.Length >= 2)
                                Login = s[1];
                        }
                        if (r["ID"] != DBNull.Value)
                            UserProfile_GUID = (string)r["ID"].ToString();
                        if (r["ID1C"] != DBNull.Value)
                            ID1C = (string)r["ID1C"];
                        if (r["ImageUrl"] != DBNull.Value)
                            ImageUrl = (string)r["ImageUrl"];
                        if (r["SiteUrl"] != DBNull.Value)
                            SiteUrl = (string)r["SiteUrl"];
                        if (r["Office"] != DBNull.Value)
                        {
                            if ((string)r["Office"] == "Румянцево")
                                IsOffice = true;
                        }
                        if (r["Office"] != DBNull.Value)
                            OfficeName = (string)r["Office"];
                        if (r["Department"] != DBNull.Value)
                            DepartmentName = (string)r["Department"];
                        if (r["Staff"] != DBNull.Value)
                            jobName = (string)r["Staff"];
                        if (r["WorkPhone"] != DBNull.Value)
                            Phone = (string)r["WorkPhone"];
                        if (r["Mail"] != DBNull.Value)
                            Mail = (string)r["Mail"];

                        Exception err =
                            oListHlp.Insert(HireDate2, IsOffice, OfficeName, DepartmentName, jobName, AccountName, Login, UserProfile_GUID, ID1C, HireDate.ToString(), ImageUrl, SiteUrl, Phone, Mail);
                        if (err != null)
                        {
                        }
                    }
                }
            }

            return ret;
        }
        #endregion
        #region Data
        public Exception GetUsersLists()
        {
            Dictionary<DateTime, string> parInf = new Dictionary<DateTime, string>();
            return GetUsersLists(ref parInf);
        }
        public Exception GetUsersLists(ref Dictionary<DateTime, string> parInf)
        {
            Exception ret = null;

            ad.MakeTableUserExtProp(ref BirthdaysUsersOfficeTbl);
            ret = ad.GetUsersInOffice(ref adUsersOfficeTbl, "Румянцево", true, true);
            if (ret != null)
            {
                parInf.Add(DateTime.Now, ret.Message);
                return ret;
            }
            parInf.Add(DateTime.Now, "UsersOfficeTbl count - " + adUsersOfficeTbl.Rows.Count);

            ad.MakeTableUserExtProp(ref adUsersNotOfficeTbl);
            ret = ad.GetUsersInNotOffice(ref adUsersNotOfficeTbl, "Румянцево", true, true);
            if (ret != null)
            {
                parInf.Add(DateTime.Now, ret.Message);
                return ret;
            }
            parInf.Add(DateTime.Now, "UsersNotOfficeTbl count - " + adUsersNotOfficeTbl.Rows.Count);

            return ret;
        }
        public Exception GetDateBirth(ref string parRet, string ID1C)
        {
            return OneC.GetDateBirth(ref parRet, ID1C);
        }
        public Exception GetDateArb(ref string parRet, string ID1C)
        {
            return OneC.GetDateArb(ref parRet, ID1C);
        }
        public Exception SPList_BirthdaysAll_DeleteAll()
        {
            Ko2.Portal.SP_List.SPList_BirthdaysAll o =
                new Ko2.Portal.SP_List.SPList_BirthdaysAll();
            return o.DeleteAll();
        }
        public Exception InsertErrSPUsers(DateTime DT, DataRow r)
        {
            Exception ret = null;
            Ko2.Portal.SP_List.SPList_errSPUsers oListHlp = new Ko2.Portal.SP_List.SPList_errSPUsers();
            string[] splitChar = { @"\" };
            string AccountName = "";
            string Login = "";
            string UserProfile_GUID = "";
            string ID1C = "";
            string HireDate = "";
            string ImageUrl = "";
            string SiteUrl = "";
            if (r["AccountName"] != DBNull.Value)
                AccountName = (string)r["AccountName"];
            if (!String.IsNullOrEmpty(AccountName))
            {
                string[] s = AccountName.Split(splitChar, StringSplitOptions.None);
                if (s.Length >= 2)
                    Login = s[1];
            }
            if (r["ID"] != DBNull.Value)
                UserProfile_GUID = (string)r["ID"].ToString();
            if (r["ID1C"] != DBNull.Value)
                ID1C = (string)r["ID1C"];
            if (r["HireDate"] != DBNull.Value)
                HireDate = (string)r["HireDate"];
            if (r["ImageUrl"] != DBNull.Value)
                ImageUrl = (string)r["ImageUrl"];
            if (r["SiteUrl"] != DBNull.Value)
                SiteUrl = (string)r["SiteUrl"];
            oListHlp.Insert(DT, AccountName, Login, UserProfile_GUID, ID1C, HireDate, ImageUrl, SiteUrl);
            return ret;
        }
        #endregion

    }

}
